package com.ayuku.retail.ui.stock;

import java.awt.Font;
import java.io.File;
import java.util.logging.Logger;

import net.sourceforge.barbecue.Barcode;
import net.sourceforge.barbecue.BarcodeFactory;
import net.sourceforge.barbecue.BarcodeImageHandler;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.ayuku.commons.AyBigDecimal;
import com.ayuku.commons.Util;

/**
 * @author putukn@gmail.com
 */
public class CompStock extends org.eclipse.swt.widgets.Composite {

	protected Text txtName;
	private Label label11;
	public Button chkBisaDiedit;
	private Label label10;
	protected Text txtUnit;
	protected Text txtCategory;
	protected Text txtBarcode;
	private Label label9;
	protected AyBigDecimal txtQtyOnHand;
	private Label label8;
	protected AyBigDecimal txtPrice;
	protected AyBigDecimal txtDisc;
	protected AyBigDecimal txtReorderPoint;
	protected AyBigDecimal txtSellingPrice;
	private Label label7;
	private Label label5;
	private static Logger LOG = Logger.getLogger(CompStock.class.getName());
	private Label lblBarcode;

	public CompStock(final org.eclipse.swt.widgets.Composite parent,
			final int style) {
		super(parent, style);
		initGUI();
	}

	private void initGUI() {
		try {
			this.setBackground(SWTResourceManager.getColor(239, 235, 239));
			setLayout(new GridLayout(2, false));

			final Label label1 = new Label(this, SWT.NONE);
			label1.setText("Kode:");
			label1.setAlignment(SWT.RIGHT);

			label1.setBackground(SWTResourceManager.getColor(239, 235, 239));

			txtBarcode = new Text(this, SWT.BORDER);
			txtBarcode.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
					false, 1, 1));
			txtBarcode.setTextLimit(16);

			txtBarcode.addModifyListener(new ModifyListener() {
				public void modifyText(ModifyEvent evt) {
					txtBarcodeModifyText(evt);
				}
			});

			label10 = new Label(this, SWT.NONE);
			label10.setText("Barcode:");
			label10.setAlignment(SWT.RIGHT);
			label10.setBackground(SWTResourceManager.getColor(239, 235, 239));

			lblBarcode = new Label(this, SWT.NONE);
			lblBarcode.setBackground(org.eclipse.wb.swt.SWTResourceManager
					.getColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
			GridData gd_lblBarcode = new GridData(SWT.FILL, SWT.CENTER, false,
					false, 1, 1);
			gd_lblBarcode.heightHint = 31;
			lblBarcode.setLayoutData(gd_lblBarcode);
			lblBarcode.setText(" ");

			final Label label2 = new Label(this, SWT.NONE);
			label2.setText("Nama Barang:");
			label2.setAlignment(SWT.RIGHT);

			label2.setBackground(SWTResourceManager.getColor(239, 235, 239));

			txtName = new Text(this, SWT.BORDER);
			txtName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
					false, 1, 1));
			txtName.setTextLimit(50);

			final Label label3 = new Label(this, SWT.NONE);
			label3.setText("Harga Beli (Rp.):");
			label3.setAlignment(SWT.RIGHT);

			label3.setBackground(SWTResourceManager.getColor(239, 235, 239));

			{
				txtPrice = new AyBigDecimal(this, SWT.BORDER);
				txtPrice.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
						false, 1, 1));
				txtPrice.setStringFormatter("###,###,###.##");

			}

			label5 = new Label(this, SWT.NONE);
			label5.setText("Harga Jual (Rp.):");
			label5.setAlignment(SWT.RIGHT);
			label5.setBackground(SWTResourceManager.getColor(239, 235, 239));

			txtSellingPrice = new AyBigDecimal(this, SWT.BORDER);
			txtSellingPrice.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
					true, false, 1, 1));

			txtSellingPrice.setStringFormatter("###,###,###.##");

			final Label label4 = new Label(this, SWT.NONE);
			label4.setText("Kategori:");
			label4.setAlignment(SWT.RIGHT);

			label4.setBackground(SWTResourceManager.getColor(239, 235, 239));

			txtCategory = new Text(this, SWT.BORDER);
			txtCategory.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
					false, 1, 1));
			txtCategory.setTextLimit(30);

			final Label label6 = new Label(this, SWT.NONE);
			label6.setText("Satuan:");
			label6.setAlignment(SWT.RIGHT);

			label6.setBackground(SWTResourceManager.getColor(239, 235, 239));

			txtUnit = new Text(this, SWT.BORDER);
			txtUnit.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
					false, 1, 1));
			txtUnit.setTextLimit(20);

			label7 = new Label(this, SWT.NONE);
			label7.setText("Reorder Poin:");
			label7.setAlignment(SWT.RIGHT);
			label7.setBackground(SWTResourceManager.getColor(239, 235, 239));

			txtReorderPoint = new AyBigDecimal(this, SWT.BORDER);
			txtReorderPoint.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
					true, false, 1, 1));
			txtReorderPoint.setStringFormatter("###,###.##");

			label8 = new Label(this, SWT.NONE);
			label8.setText("Jumlah tersedia:");
			label8.setAlignment(SWT.RIGHT);
			label8.setBackground(SWTResourceManager.getColor(239, 235, 239));

			txtQtyOnHand = new AyBigDecimal(this, SWT.BORDER);
			txtQtyOnHand.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
					false, 1, 1));

			txtQtyOnHand.setStringFormatter("###,###.##");

			label11 = new Label(this, SWT.NONE);
			label11.setText("Harga jual bisa diedit");
			label11.setAlignment(SWT.RIGHT);
			label11.setBackground(SWTResourceManager.getColor(239, 235, 239));

			chkBisaDiedit = new Button(this, SWT.CHECK | SWT.LEFT);
			chkBisaDiedit.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER,
					true, false, 1, 1));

			chkBisaDiedit.setBackground(SWTResourceManager.getColor(171, 209,
					252));

			label9 = new Label(this, SWT.NONE);
			label9.setText("Diskon (%):");
			label9.setAlignment(SWT.RIGHT);
			label9.setBackground(SWTResourceManager.getColor(239, 235, 239));

			txtDisc = new AyBigDecimal(this, SWT.BORDER);
			txtDisc.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
					false, 1, 1));

			this.layout();

		} catch (Exception e) {
			e.printStackTrace();
			LOG.info(e.getMessage());
			Util.showMsgBox("Gagal load: " + e.getMessage(), 1, getShell());
		}
	}

	private void txtBarcodeModifyText(ModifyEvent evt) {

		String bar = txtBarcode.getText();
		if (bar == null || bar.trim() == "") {
			bar = "kosong";
			lblBarcode.setImage(null);
		} else {
			try {
				Barcode barcode = BarcodeFactory.createCode128(bar);

				File f = new File("tes.gif");
				// barcode.setSize(40, 20);
				barcode.setBarHeight(2);
				barcode.setResolution(72);
				barcode.setBarWidth(1);
				// barcode.setDoubleBuffered(true);
				// barcode.setLabel("Mangga");
				barcode.setFont(new Font("sansserif", Font.PLAIN, 10));

				BarcodeImageHandler.saveGIF(barcode, f);
				String s = f.getAbsolutePath();
				Image img = new Image(getShell().getDisplay(), s);

				lblBarcode.setImage(img);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}
}
